package com.wh.wisdomsite.xbox.utils.json.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.wh.wisdomsite.xbox.utils.json.JosnConverter;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
 * JSON转换(com.alibaba.fastjson接口实现)
 * @Package com.wh.wisdomsite.xbox.utils.json
 * @author 谢泽鹏
 * @date 2014年10月8日 下午4:02:14
 * @Copyright 个人版权所有
 * @Company 贵州万华科技有限公司Copyright (c) 2014
 * @version V1.0
 */
public class FastJsonConverter implements JosnConverter {

    private static final Log logger = LogFactory.getLog(FastJsonConverter.class);

    private static SerializerFeature[] features;

    static {
        features = new SerializerFeature[2];
        features[0] = SerializerFeature.PrettyFormat;
        features[1] = SerializerFeature.WriteNullStringAsEmpty;

    }

    /**
     * 将String转换成Object
     * @param string 转换string
     * @param clazz  转换object
     * @param <T>    返回结果
     * @return
     */
    @Override
    public <T> T getObject(String string, Class<T> clazz) {
        return JSON.parseObject(string, clazz);
    }


    /**
     * 将Objcet转换成String
     * @param object 转换object
     * @return 返回结果
     */
    @Override
    public String getString(Object object) {
        return JSON.toJSONString(object, features);
    }
}
